#define RUNDIR "/opt/apps/top.yzzi.wallpaper/files/"
#include "filechoosewidget.h"
#include <DFileDialog>
#include <QMimeData>
#include <QDragEnterEvent>
#include <QFileInfo>
#include <QPixmap>
#include <QDebug>
#include <DLabel>
#include <QImageReader>
#include <QVBoxLayout>
#include <DApplicationHelper>
#include <sstream>
#include "utils.h"
#include "mainwindow.h"
//以下大量代码来自https://github.com/linuxdeepin/deepin-deb-installer

using namespace std;
FileChooseWidget::FileChooseWidget(QWidget *parent) :
    QWidget(parent)
{
    this->setAcceptDrops(true);
    m_iconImage = new DLabel(this);//图标
    m_iconImage->setFixedSize(110, 110);
    m_iconImage->setPixmap(Utils::renderSVG(":/new/prefix1/images/picture.svg", QSize(110, 110)));


    m_dndTips = new DLabel(this);//提示文字
    m_dndTips->setFixedHeight(30);
    m_dndTips->setAlignment(Qt::AlignTop);
    m_dndTips->setText(tr("把视频丢进来(>▽<)"));
    m_dndTips->setObjectName("DNDTips");


    DGuiApplicationHelper *guiAppHelp = DGuiApplicationHelper::instance();
    split_line = new DLabel(this);
    split_line->setObjectName("SplitLine");//分隔线
    if (guiAppHelp->themeType() == DGuiApplicationHelper::ColorType::DarkType) {  //暗色主题
        split_line->setPixmap(Utils::renderSVG(":/new/prefix1/images/split_line_dark.svg", QSize(220, 3)));
    } else {  //浅色主题
        split_line->setPixmap(Utils::renderSVG(":/new/prefix1/images/split_line.svg", QSize(220, 3)));
    }
    split_line->setFixedHeight(3);


    m_chooseFileBtn = new ChooseFileButton("", this);
    m_chooseFileBtn->setFixedHeight(28);
    m_chooseFileBtn->setObjectName("FileChooseButton");
    m_chooseFileBtn->setText(tr("点我选择文件"));
#ifdef SHOWBORDER
    m_chooseFileBtn->setStyleSheet("QPushButton{border:1px solid black;}");
#endif


    QVBoxLayout *centralLayout = new QVBoxLayout(this);
    centralLayout->addSpacing(40);
    centralLayout->addWidget(m_iconImage);
    centralLayout->setAlignment(m_iconImage, Qt::AlignHCenter);

    centralLayout->addSpacing(30);
    centralLayout->addWidget(m_dndTips);
    centralLayout->setAlignment(m_dndTips, Qt::AlignHCenter);

    centralLayout->addWidget(split_line);
    centralLayout->setAlignment(split_line, Qt::AlignHCenter);

    centralLayout->addSpacing(11);
    centralLayout->addWidget(m_chooseFileBtn);
    centralLayout->setAlignment(m_chooseFileBtn, Qt::AlignHCenter);
    centralLayout->addStretch();

    centralLayout->setSpacing(0);
    centralLayout->setContentsMargins(0, 0, 0, 0);

    setLayout(centralLayout);
    connect(m_chooseFileBtn, &ChooseFileButton::clicked, this, &FileChooseWidget::chooseFiles);

}

void FileChooseWidget::dragEnterEvent(QDragEnterEvent *e)
{
    //对拖放事件进行筛选
    if (true)
    {
        e->acceptProposedAction();	//放行，否则不会执行dropEvent()函数
    }
}


void FileChooseWidget::dropEvent(QDropEvent *e)
{
    foreach(auto item,pathlist)
    {
        pathlist.removeOne(item);
        qDebug()<<"qlist.size()="<<pathlist.size();
    }
    QList<QUrl> urls = e->mimeData()->urls();
    if(urls.isEmpty())
        return ;
    qDebug()<< urls.size();
    foreach (QUrl u, urls)
    {
        QString filepath = u.toLocalFile();
        pathlist.append(filepath);
    }
    pathlist = pathlist.toSet().toList();
    emit FileChooseWidget::setwallpaper(pathlist);
}



void FileChooseWidget::chooseFiles()//选择文件
{
    QString historyDir = m_settings.value("history_dir").toString();

    if (historyDir.isEmpty()) {
        historyDir = QDir::homePath();
    }

    DFileDialog dialog;
    dialog.setFileMode(QFileDialog::ExistingFiles);
    dialog.setNameFilter("Video Files (*.mp4 *.m4v *.avi)");
    dialog.setDirectory(historyDir);

    const int mode = dialog.exec();

    // save the directory string to config file.
    m_settings.setValue("history_dir", dialog.directoryUrl().toLocalFile());

    if (mode != QDialog::Accepted) return;

    const QStringList selected_files = dialog.selectedFiles();

    emit FileChooseWidget::setwallpaper(selected_files);
}



void FileChooseWidget::setwallpaper(QStringList selected_files)
{
    system("mkdir ~/.config/OneWallpaper/");
    qDebug()<<selected_files;
    ostringstream mklk;
    QString tmp;
    QString qmklk;
    tmp = selected_files.at(0);
    qDebug()<<"tmp ="<< tmp;
    mklk.str("");
    mklk<<"cp -f \""<<tmp.toStdString()<<"\" ~/.config/OneWallpaper/video";
    qmklk = QString::fromStdString(mklk.str());
    qDebug()<<qmklk;
    system(qmklk.toUtf8());
    FileChooseWidget::runxwinwrap();
    }

void FileChooseWidget::runxwinwrap()
{
    FileChooseWidget::killxwinwrap();
    if(sound==true)
    {
        system("killall dde-desktop");
        system("nohup /opt/apps/top.yzzi.wallpaper/files/dde-desktop &");
    }
    else
    {
        system("killall dde-desktop");
        system("nohup /opt/apps/top.yzzi.wallpaper/files/dde-desktop &");
    }
}

void FileChooseWidget::killxwinwrap()
{
    system("killall dde-desktop");
    system("nohup dde-desktop &");
}

FileChooseWidget::~FileChooseWidget()
{

}

